<?php
class CustUsersController extends AppController {
    var $name = 'CustUsers';

    var $pageTitle = 'Customer User Information';

    var $viewPath = 'customers/admins/users';

    var $uses = array('User', 'Customer', 'Site');

    var $helpers = array('Html', 'Form', 'Ajax', 'My');

    function beforeFilter() {
        parent::beforeFilter();
        $this->set('parentNav', 'cust_admins');
        $this->set('tabMenu', 'customer/CustAdmins/tabMenus');
    }

    function display() {
        parent::display();
        if ($this->Auth->user('is_head_user')) {    // ถ้าเป็น Head office user จะดูข้อมูลได้ทุก site
            $this->data['User']['customer_id'] = $this->Auth->user('customer_id');
        } else {                                    // Site user ดูข้อมูลได้เฉพาะ site ตัวเอง
            $this->data['User']['site_id'] = $this->Auth->user('site_id');
        }
        $this->_paging('User', 'LIKE');
    }
    
    function save(){
        $user_id = null;
        if ($this->data) {
            $is_success = false;
            // ฟิลด์ password ที่มี username จะมีการเข้ารหัสอัตโนมัติ

            if(!empty($this->data['User']['password'])){
                if ($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm'])) {
                    $is_success = $this->_save_user();
                } else {
                    $this->User->validationErrors['password'] = 'Password did not match.';
                }
            } else {
                $is_success = $this->_save_user();
            }

            if($is_success) {
                $this->Session->setFlash("Save user succesfull.", 'flash/ok');
                $user_id = $this->User->id;
            } else {
                $this->Session->setFlash("Save user fail.", 'flash/fail');
                $user_id = $this->data['User']['id'];
            }
        }

        $this->data['User']['password'] = '';
        $this->data['User']['password_confirm'] = '';

        $this->setAction('view', $user_id);
    }

    private function _save_user() {
        $this->User->create();
        // ถ้า Validation ถูกต้อง บันทึกข้อมูลได้
        $this->data['User']['prefix'] = CUST_PREFIX; // Prefix เป็น Customer อย่างเดียว

        $this->data['User']['collector_id'] = $this->Auth->user('id');
        $this->data['User']['customer_id'] = $this->Auth->user('customer_id');
        if($this->User->save($this->data)) {
            return true;
        } else{
            return false;
        }
    }

    function view($id = null) {
        // edit
        if ($id != null) {
            $this->data = $this->User->findById($id);
            $this->data['User']['password'] = '';
            $this->data['User']['password_confirm'] = '';

            $this->User->id = $id;
            $this->set('neighbors', $this->User->find('neighbors', array(
                        'fields' => array('id', 'name'),
                        'conditions' => $this->Auth->user('is_head_user')? 
                            array('User.customer_id' => $this->Auth->user('customer_id')) :
                            array('User.site_id' => $this->Auth->user('site_id'))
                    )
                )
            );
            $this->set('mode','edit');
        } // else add
        else{
            $this->set('mode','add');
        }
    }

    function delete($id = null) {
        // จะ error ถ้าลบ category ที่มี product อยู่
        Configure::write('debug', 0);
        if ($this->User->delete($id, false)) {
            $this->Session->setFlash("User delete succesfull.", 'flash/ok');
        } else {
            $this->Session->setFlash("User delete fail.", 'flash/fail');
        }
        $this->setAction('display');
    }

    function popup_search() {
        parent::popup_search();

        $this->set('customers', $this->User->Customer->find('list', array('fields' => array('Customer.id', 'Customer.name'))));
        $this->set('sites', $this->User->Site->find('list', array('fields' => array('Site.id', 'Site.name'),)));

    }

}
